package edu.bit.water.service;


import edu.bit.water.dao.WasteWaterEvaDao;
import edu.bit.water.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;
import java.util.*;

/**
 * meta_assessment_indicator数据表对应的service类
 * @Author: Page
 */
@Service
public class WasteWaterEvaService {

    @Autowired
    private WasteWaterEvaDao WasteWaterEvaDao;

    @Autowired
    private EntityEffluentLetService entityEffluentLetService;

    public List<WasteWaterEva> selectWasteWaterEva() {
        return WasteWaterEvaDao.selectWasteWaterEva();
    }

    public List<WasteWaterEva2> selectWasteWaterEva2() {
        return WasteWaterEvaDao.selectWasteWaterEva2();
    }

    public Map<String, Object> insertWasteWaterEva(WasteWaterEva WasteWaterEva) {
        Map<String, Object> result = new HashMap<>();
        Integer id = WasteWaterEvaDao.getMaxId();
        if (id != null) {
            WasteWaterEva.setId(id + 1);
        } else {
            WasteWaterEva.setId(0);
        }
        int tmp = WasteWaterEvaDao.insertWasteWaterEva(WasteWaterEva);
        if (tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    public Map<String, Object> insertWasteWaterEva2(WasteWaterEva2 WasteWaterEva2) {
        Map<String, Object> result = new HashMap<>();
        Integer id = WasteWaterEvaDao.getMaxId2();
        if (id != null) {
            WasteWaterEva2.setId2(id + 1);
        } else {
            WasteWaterEva2.setId2(0);
        }
        int tmp = WasteWaterEvaDao.insertWasteWaterEva2(WasteWaterEva2);
        if (tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    public Map<String, Object> deleteWasteWaterEva(List<String> ids) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < ids.size(); i++) {
            int temp = entityEffluentLetService.deleteByWasteWaterId(Integer.parseInt(ids.get(i)));
            //System.out.println(temp);
            int tmp = WasteWaterEvaDao.deleteWasteWaterEva(Integer.parseInt(ids.get(i)));
            //System.out.println(tmp);
            if (tmp > 0 && temp >= 0) {
                result.put(ids.get(i).toString(), "success");
            }
            else {
                result.put(ids.get(i).toString(), "failed");
                flag = false;
            }
        }
        if (flag) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    public Map<String, Object> deleteWasteWaterEva2(List<String> ids) {
        Map<String, Object> result = new HashMap<>();
        boolean flag = true;
        for (int i = 0; i < ids.size(); i++) {
            // ?????
            int temp = entityEffluentLetService.deleteByWasteWaterId(Integer.parseInt(ids.get(i)));
            //System.out.println(temp);
            int tmp = WasteWaterEvaDao.deleteWasteWaterEva2(Integer.parseInt(ids.get(i)));
            //System.out.println(tmp);
            if (tmp > 0 && temp >= 0) {
                result.put(ids.get(i).toString(), "success");
            }
            else {
                result.put(ids.get(i).toString(), "failed");
                flag = false;
            }
        }
        if (flag) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }


    public Map<String, Object> updateWasteWaterEva(WasteWaterEva WasteWaterEva) {
        Map<String, Object> result = new HashMap<>();
        int tmp = WasteWaterEvaDao.updateWasteWaterEva(WasteWaterEva);
        if(tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    public Map<String, Object> updateWasteWaterEva2(WasteWaterEva2 WasteWaterEva2) {
        Map<String, Object> result = new HashMap<>();
        int tmp = WasteWaterEvaDao.updateWasteWaterEva2(WasteWaterEva2);
        if(tmp == 1) {
            result.put("status", "success");
        }
        else {
            result.put("status", "failed");
        }
        return result;
    }

    public List<MetaDischarge> getBasis() {
        List<MetaDischarge> list = WasteWaterEvaDao.getBasis();
        return list;
    }
    public List<MetaDischargeMaxPermission> getScope() {
        List<MetaDischargeMaxPermission> list = WasteWaterEvaDao.getScope();
        return list;
    }
    public List<MetaDischargeMaxPermission> getLevel() {
        List<MetaDischargeMaxPermission> list = WasteWaterEvaDao.getLevel();
        return list;
    }

    /**
     * 查询META_WATER_SAVING中的标准状态
     * @return META_WATER_SAVING中的所有记录
     */
    public List<MetaDischarge> getStandardStatus() {
        List<MetaDischarge> list = WasteWaterEvaDao.getStandardStatus();
        return list;
    }
}